﻿using AVCUMPlatform.BLL.Interface;
using AVCUMPlatform.BLL;
using AVCUMPlatform.Entities;
using AVCUMPlatform.Models;
using Microsoft.AspNetCore.Mvc;
using AVCUMPlatform.HelperLibrary;

namespace AVCUMPlatform.Controllers
{
    /// <summary>
    /// 全局设置接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SystemInfoController : ControllerBase
    {
        private ISystemInfoBLL _systemInfoBLL = BLLContainer.Resolve<ISystemInfoBLL>();

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NoNeedLogin]
        public ActionResult GetList()
        {
            var list = _systemInfoBLL.GetAll();
            var obj = new { code = 0, data = list, msg = "" };
            return new JsonResult(obj);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="systemInfos"></param>
        /// <returns></returns>
        [HttpPost]
        [NoNeedLogin]
        public ResultModel AddOrUpdate([FromBody] List<SystemInfo> systemInfos)
        {
            var ret = new ResultModel();
            foreach (var systemInfo in systemInfos)
            {
                var exist = _systemInfoBLL.GetAll().FirstOrDefault(x => x.Code == systemInfo.Code);
                if (exist != null && exist.Id > 0)
                {
                    exist.Name = systemInfo.Name;
                    exist.Value = systemInfo.Value;
                    exist.ModifyTime = DateTime.Now.ToYMDHMS();
                    _systemInfoBLL.Update(exist);
                }
                else
                {
                    systemInfo.CreateTime = DateTime.Now.ToYMDHMS();
                    _systemInfoBLL.Add(systemInfo);
                }
            }
            ret.code = 1;
            return ret;
        }


    }
}
